package com.zzgele.qmzb.api.web;

import com.alibaba.boot.dubbo.annotation.DubboConsumer;
import com.github.pagehelper.PageInfo;
import com.zzgele.qmzb.api.core.Result;
import com.zzgele.qmzb.api.core.ResultCode;
import com.zzgele.qmzb.api.core.ResultGenerator;
import com.zzgele.qmzb.api.service.VideoLikeService;
import com.zzgele.qmzb.api.service.VideoListService;
import com.zzgele.qmzb.api.utils.DateUtil;
import com.zzgele.qmzb.api.utils.PageUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * create by jack
 */
@RequestMapping("/video")
@RestController
public class VideoListController {
    private final Logger logger = LoggerFactory.getLogger(VideoListController.class);
    @DubboConsumer
    private VideoListService videoListService;

    @DubboConsumer
    private VideoLikeService videoLikeService;

    @PostMapping("/dislike")
    public Result dislike(Long login_uid,Long videoId){
        HashMap map = new HashMap();
        map.put("uid",login_uid);
        map.put("vid",videoId);
        int a=videoLikeService.dislike(map);

        return ResultGenerator.genSuccessResult();
    }
    @PostMapping("/like")
    public Result like(Long login_uid,Long videoId){
        if(login_uid!=null&&videoId!=null) {
            HashMap map = new HashMap();
            map.put("uid", login_uid);
            map.put("vid", videoId);
            map.put("create_time", DateUtil.getCurUnixTimeStampInt());

            int a = videoLikeService.like(map);
            if(1==a){
                Result result =new Result();
                result.setCode(ResultCode.REPEAT_LIKE).setMessage("重复点赞");
                return result;
            }
        }
        return ResultGenerator.genSuccessResult();

    }
    @PostMapping("/delete")
    public Result delete(Long login_uid,Long videoId){
        if(login_uid!=null&&videoId!=null) {
            HashMap map = new HashMap();
            map.put("id", videoId);
            int a = videoListService.deleteVideo(map);
        }
        return ResultGenerator.genSuccessResult();

    }
    @PostMapping("/my/list")
    public Result list(Long login_uid,Integer page,Integer page_size) {
        if(page==null)
            page=1;
        if(page_size==null){
            page_size=20;
        }
        PageUtil.startPage(page, page_size);
        HashMap map = new HashMap();
        map.put("uid",login_uid);
        List<HashMap> list = videoListService.getList(map);
        for(HashMap item:list){
            String s=(String)item.get("file_name");
            item.put("fileName",OssController.Bucket.get("qmwlzb-one")+s);
            item.remove("file_name");
            //预留字段
            item.put("shareUrl","");
            item.put("isLike",0);
            item.put("commentNum",0);

        }
        HashMap videoListHashMap = PageUtil.getPageInfoHashMap(new PageInfo(list));

        return ResultGenerator.genSuccessResult(videoListHashMap);
    }
    //查询所有和别人的小视频
    @PostMapping("/getVideoList")
    public Result getVideoList(Long login_uid,Long uid,Integer page,Integer page_size) {


        HashMap map = new HashMap();
        map.put("login_uid",login_uid);
        map.put("uid",uid);
        map.put("status",1);

//        List<HashMap> list = videoListService.getList(map);
        List<HashMap> list = videoListService.getListVideo(map,page,page_size);
        for(HashMap item:list){
            String s=(String)item.get("file_name");
            item.put("fileName",OssController.Bucket.get("qmwlzb-one")+s);
            item.remove("file_name");
            //预留字段
            item.put("shareUrl","");
            item.put("commentNum",0);

        }
        HashMap videoListHashMap = PageUtil.getPageInfoHashMap(new PageInfo(list));

        return ResultGenerator.genSuccessResult(videoListHashMap);
    }
}
